import { prisma } from "@/lib/prisma"
import { ProductList } from "@/components/products/product-list"
import { ProductHeader } from "@/components/products/product-header"
import { Metadata } from "next"
import { Product, Category } from "@prisma/client"
import { Decimal } from "@prisma/client/runtime/library"

export const metadata: Metadata = {
  title: "商品管理 - 商场销售系统",
  description: "商品管理页面",
}

// 创建一个类型来处理序列化后的数据
type SerializedProduct = Omit<Product, 'price' | 'createdAt' | 'updatedAt'> & {
  price: string
  createdAt: string
  updatedAt: string
  category: Omit<Category, 'createdAt' | 'updatedAt'> & {
    createdAt: string
    updatedAt: string
  }
}

// 序列化函数
function serializeProduct(product: Product & { category: Category }): SerializedProduct {
  return {
    ...product,
    price: (product.price as unknown as Decimal).toString(),
    createdAt: product.createdAt.toISOString(),
    updatedAt: product.updatedAt.toISOString(),
    category: {
      ...product.category,
      createdAt: product.category.createdAt.toISOString(),
      updatedAt: product.category.updatedAt.toISOString(),
    }
  }
}

export default async function ProductsPage() {
  const products = await prisma.product.findMany({
    include: {
      category: true,
    },
    orderBy: {
      createdAt: 'desc'
    }
  })

  // 序列化产品数据
  const serializedProducts = products.map(serializeProduct)

  return (
    <div className="space-y-4 p-8">
      <ProductHeader />
      <ProductList products={serializedProducts} />
    </div>
  )
} 